/**
 * 
 */
package com.activities;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
import com.Adapters.NotificationAdapter;
import com.Animations.AnimUtil;
import com.Animations.AnimationLists;
import com.Data.Notifications;
import com.Data.Notifications.onGetNotificationsCompleted;
import com.Managers.FragmentManagerClass;
import com.Managers.ThemeManager;
import com.PopUps.PopUpClass;
import com.TextFormat.UtilFonts;
import com.shareup.R;


/**
 * @author Chris
 * 
 */
public class ActivityNotifications extends Activity {

    public static void delayGet(final Notifications not) {

        Handler myHandler = new Handler();
        myHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

                not.execute();

            }

        }, 1000);
    }

    private NotificationAdapter adapter;

    private ListView list;

    private TableRow table;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            //
            return true;
        }
        //
        return super.onKeyUp(keyCode, event);
    }

    public void setNoItemsPopUp() {

        PopUpClass up = new PopUpClass(this);
        up.setMessage(getResources().getString(R.string.dialog_no_notifications), R.drawable.notification_normal, table);

    }

    private void addToList(Notifications[] result) {

        if (adapter == null)
            adapter = new NotificationAdapter(this, R.id.c_text_name);
        else
            adapter.clear();

        boolean changes = false;
        if (result.length != 0) {
            for (Notifications not : result) {
                if (!adapter.contains(not)) {
                    changes = true;
                    adapter.add(not);
                }

            }

            list.setAdapter(adapter);
        }
        if (changes)
            adapter.notifyDataSetChanged();

        stopLoading();

        if (adapter.getCount() == 0)
            setNoItemsPopUp();

        // RESETTING THE NOTIFICATION NUMBER
        FragmentManagerClass.aMenu.updateNotifications();

    }

    private void exit() {

        finish();
        AnimUtil.getActivityTransition(this, false);
    }

    private void getNotifications() {

        Notifications nt = new Notifications(this, Notifications.ACTION_GET);
        nt.setCallbackGet(new onGetNotificationsCompleted() {

            @Override
            public void Completed(Notifications[] result) {

                addToList(result);

            }
        });
        delayGet(nt);

        setLoading();
    }

    private void init() {

        AnimationLists.displayedComments.clear();

        ImageView backB = (ImageView) findViewById(R.id.button_back);
        backB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                exit();
            }
        });
        TextView title = (TextView) findViewById(R.id.title_actionbar);
        UtilFonts.setTextBoldFont(title);
        list = (ListView) findViewById(R.id.Listview);
        table = (TableRow) findViewById(R.id.aMine_table_tooltip);
        //
        getNotifications();
    }

    private void setLoading() {

        PopUpClass up = new PopUpClass(this);
        up.setLoadingList(table);

    }

    private void stopLoading() {

        AnimUtil.animatePopUp(table, false, null);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ThemeManager.setTheme(this);
        setContentView(R.layout.activity_notifications);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onDestroy() {

        FragmentManagerClass.aNotifications = null;
        super.onDestroy();
    }

    @Override
    protected void onResume() {

        FragmentManagerClass.aNotifications = this;
        if (adapter == null)
            init();

        super.onResume();
    }

}
